<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>04_员工列表练习</title>
</head>
<body>
<input type="text" placeholder="请输入员工名字" id="i1" ><br>
<input type="text" placeholder="请输入员工工资" id="i2" ><br>
<input type="text" placeholder="请输入员工岗位" id="i3" ><br>
<button onclick="add()">添加员工</button>
<hr>
<table border="1">
    <caption>员工列表</caption>
    <tr>
        <th>员工名字</th>
        <th>员工工资</th>
        <th>员工岗位</th>
    </tr>
</table>

<script>
    let arr = [];
    //1.定义添加员工的自定义方法
    function add(){
        //2.获取用户输入的数据
        let uName = document.getElementById('i1').value;
        let uSalary = document.getElementById('i2').value;
        let uJob = document.getElementById('i3').value;
        //3.对数据进行校验
        if(!uName||!uSalary||!uJob){
            alert('请填写完整信息');
            return;
        }
        //4.每多一个员工信息,就创建1个tr与3个td元素
        let trE = document.createElement('tr');
        let nameTd = document.createElement('td');
        let salaryTd = document.createElement('td');
        let jobTd = document.createElement('td');
        //5.将收集到的员工数据放入对应的单元格中
        nameTd.innerHTML = uName;
        salaryTd.innerHTML = uSalary;
        jobTd.innerHTML = uJob;
        //6.将3个td元素放入tr元素中
        trE.append(nameTd,salaryTd,jobTd);
        //7.获取用来展示tr的table元素
        let table = document.querySelector('table');
        //8.将tr元素放入table元素中
        table.append(trE);
        //9.清空输入框中已使用过的数据
        document.getElementById('i1').value = '';
        document.getElementById('i2').value = '';
        document.getElementById('i3').value = '';

        arr.push({
            name:uName,
            salary:uSalary,
            job:uJob
        });
        console.log(arr);
    }
</script>
</body>
</html>